!function (window, $, undefined) {
    'use strict';
    var attach_json=null;
    var json_attach=[];
    var bar = 0;
    function alertThumb(obj){
        alert(obj.attr('id'));
    }
    window.App = {
        alert : function(msg) {
            return alert(msg);
        },
        confirm : function(msg) {
            return confirm(msg);
        },
        opus : {
            tpl : {
                auto: function(aryObj) {
                    var _out = [], _tmp, _attachId = [];
                    for (var i in aryObj) {
                        try {
                            _tmp = JSON.parse(aryObj[i]);
                        } catch (e) {
                            _tmp = aryObj[i];
                        }
                        _out.push(this.div(_obj));
                    }
                    //$('#attach-id').val(_attachId.join(','));
                    return _out.join('');
                },
                div : function(obj) {
                    if (obj == undefined) {
                        obj  = {
                            name : '',
                            psdId : ''
                        }
                    }
                    //return '<div class="item5">'+
                    //'添加户型图：<input type="button" class="button" id="select" name="select" value="选择图片"/>'+
                    //'<div class="drawing">'+
                    //'<span class="img_box">'+
                    //'<span><img id="design" src=""/></span>'+
                    //'<input type="hidden" id="img_src" value=""/>'+
                    //'</span>'+
                    //'<em class="delete" style="margin-left: 40px;">删除</em>'+
                    //'</div>'+
                    //'<input id="upload_file" name="upload_file" type="file" style=" display:none;" value=""/>'+
                    //'</div>';
                }
            },
            init : function() {},
            match : function(value, ext) {
                var regx = new RegExp('\\.' + ext + '$', 'i');
                return regx.test(value);
            },
            ajaxUp : function (fileId, mode, inputname) {
                var _fileValue = $('#' + fileId).val();
                var file = document.getElementById(fileId).files[0];
                var fileSize = 0;
                fileSize = Math.round(file.size * 100 / (1024 * 1024)) / 100;
                //alert(fileSize);
                if(fileSize>8) {
                    alert("文件大于8MB,不能上传！");
                    return;
                }
                switch (fileId) {
                    case 'upload_file':
                        if (!this.match(_fileValue, '((jpg)|(jpeg)|(png))')) {
                            alert('图片只能为jpg或png格式！');
                            return;
                        }
                        break;
                    default :
                        if (!this.match(_fileValue, '((jpg)|(jpeg)|(png))')) {
                            alert('图片只能为jpg或png格式！');
                            return;
                        }
                        break;
                }
                $.ajaxFileUpload({
                    url: '/api/upload/upload',
                    type: 'post',
                    data: {'fileId':fileId, 'mode' : mode },
                    //secureuri: false,
                    fileElementId: fileId,
                    dataType: 'json',
                    success: function (data, status) {
                        switch (data.status) {
                            case 'SUCCESS' :
                                $('#InvoiceShow').empty();
                                $('#InvoiceShow').append('<img src="'+'/data/tmp/' + data.rsp.name+'" width="100%"/>');
                                $("#"+inputname+"_json").html(JSON.stringify(data.rsp));
                                $("input[name="+inputname+"]").val(data.rsp.path + "\\" + data.rsp.name);
                                break;
                            case 'ERROR':
                                alert(data.desc);
                                break;
                        }
                    },
                    error: function (data, status, e) {
                        alert('上传文件出错1');
                    }
                });
            },
            ajaxFileUp : function (fileId, mode, obj) {
                var _fileValue = $('#' + fileId).val();
                var file = document.getElementById(fileId).files[0];
                var fileSize = 0;
                fileSize = Math.round(file.size * 100 / (1024 * 1024)) / 100;
                if(fileSize>2) {
                    alert("文件大于2MB,不能上传！");
                    return;
                }
                $.ajaxFileUpload({
                    url: '/api/upload/upload',
                    type: 'post',
                    data: {'fileId':fileId,'mode':mode},
                    //secureuri: false,
                    fileElementId: fileId,
                    dataType: 'json',
                    success: function (data, status) {
                        switch (data.status) {
                            case 'SUCCESS' :
                                $("#"+obj+"_json").html(JSON.stringify(data.rsp));
                                $("#img-target").attr("src", '/data/tmp/' + data.rsp.name);
                                $("#img-target").css("display", 'block');
                                $("#img-url").val(data.rsp.path + "\\" + data.rsp.name);
                                break;
                            case 'ERROR':
                                alert(data.desc);
                                break;
                        }
                    },
                    complete: function(xmlHttpRequest) {
                        $("#"+obj).on("change", function(){
                            var obj = $(this);
                            App.opus.ajaxFileUp(obj.attr('id'), 'uuid', obj.attr('id'));
                            return false;
                        });
                    },
                    error: function (data, status, e) {
                        alert('上传文件出错2');
                    }
                });
            }
        }
    };
    App.opus.init();

}(window,jQuery);


